import Vue from "vue";
import Vuex from 'vuex'

// 使用vuex组件
Vue.use(Vuex)

// 必须在这里引入vuex，因为import语句会在use之前先执行，所以import store会在use之前执行，会报错，所以干脆在store里引入

const actions = {
    searchByKeyword(context,value){
        // for(let event of context.state.event_list){
        //     console.log(event.id)
        //     if(event.title.includes(context.state.keyword)){
        //         console.log(2)
        //         context.commit('FILTER_QUALIFIED_ARRAYS',event)
        //
        //     }
        //     else {
        //         console.log(1)
        //         context.commit('EMPTY_ARRAY',event)
        //     }
        //
        // }
        // context.commit('FILTER_QUALIFIED_ARRAYS',value)


    },
    search(context,value){

        if(context.state.keyword!==''){
            context.commit('SEARCH_BY_KEYWORD',context.state.keyword)
        }

        if(context.state.city!==''){
            context.commit('SEARCH_BY_CITY',context.state.city)
        }

        if(context.state.date!==''){
            context.commit('SEARCH_BY_DATE',context.state.date)
        }
        if(context.state.topics!==[]){
            context.commit('SEARCH_BY_TOPICS',context.state.topics)
        }





    }
}
const mutations = {
    SEARCH_BY_KEYWORD(state,value){

        // let i ={}
        // for (i of state.event_list){
            // if(i===value){
            //     let index = state.event_list.indexOf(i)
            //     console.log('要删除'+state.event_list[index].title+'了')
            //     state.event_list.splice(value,1)
            // }
        // }

        state.event_list = state.event_list.filter((event) => {
            if(event.title.includes(value)){
                return event
            }
        })


    },
    SEARCH_BY_CITY(state,value){
        state.event_list = state.event_list.filter((event)=>{if(event.city===value)
            return event})
    },
    // MODIFY_DATE(state,value){
    //     state.date = value
    // },
    SEARCH_BY_DATE(state,value){

        state.event_list = state.event_list.filter((event)=>{if(event.date===value)
            return event})

    },
    SEARCH_BY_TOPICS(state,value){
        state.event_list = state.event_list.filter((event)=>{if(event.topics.sort().toString().includes(value.sort().toString()))
            return event})
    }
}
const state = {
    login_account: '',
    login_password:'',

    event_list:[{
        id:'001',
        cover_url:'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
        date:'2022年·11月27日',
        title:'我是标题我是标题我是标题我是标题我是标题我是标题我是标题我是标题我是标题',
        topics:["医疗器材","半导体"],
        city:'张家港',
        going:'25'
    },
        {
            id:'002',
            cover_url:'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg',
            date:'2022年·11月26日',
            title:'我是我是我是我是我是我是我是我是我是',
            topics:["纳米材料"],
            city:'苏州',
            going:'25'
        }
    ],

    // 用于筛选，即输入或选择的内容
    keyword:'',
    city:'',
    date:'',
    topics:[],

    // 本身有的选项
    tags:[{
        value: '医疗器材',
        label: '医疗器材'
    }, {
        value: '半导体',
        label: '半导体'
    }, {
        value: '纳米材料',
        label: '纳米材料'
    }],

    // 用于新建活动
    editedEvent:{
        city:'',
        address:'',
        Date:'',
        topics:[],
        title: '',
        cover_url:'',
        content:'',
        going:'0',
        id:'003',
    }

}

export default new Vuex.Store({
    actions,
    mutations,
    state
})
